package com.zzti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zzti.pojo.OrderWaiter;
import com.zzti.serviceImpl.OrderServiceImpl;
import com.zzti.serviceImpl.OrderWaiterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/order")
public class OrderController {


    Integer TotalCount = 0;

    @Autowired
    private OrderServiceImpl orderService;

    @Autowired
    private OrderWaiterServiceImpl orderWaiterService;

    @RequestMapping("/iconThree")
    @ResponseBody
    public String iconthree() {
        TotalCount = 0;
        Map<String, Integer> goods = orderService.selectGoodsCost();
        TotalCount += goods.get("value");
        Map<String, Integer> equ = orderService.selectEquCost();
        TotalCount += equ.get("value");
        Map<String, Integer> visi = orderService.selectVisiterCost();
        TotalCount += visi.get("value");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(equ);
        jsonArray.add(goods);
        jsonArray.add(visi);
        return jsonArray.toJSONString();
    }

    @RequestMapping("/iconSix")
    @ResponseBody
    public String iconSix() {
        List<Map<String, Integer>> maps = orderService.selectThisYear();
        List<Map<String, Integer>> mapsl = orderService.selectLastYear();

        System.out.println(maps);
        Integer[] s1 = {new Date().getYear() + 1900, new Date().getYear() - 1 + 1900};
        Integer[] s2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        Integer[] s3 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        Map<String, Integer> genderCount = new HashMap<>();
        for (Map<String, Integer> kv : maps) {
            String key = null;
            Integer value = null;

            for (Map.Entry<String, Integer> entry : kv.entrySet()) {
                if (entry.getKey().equals("key")) {
                    key = String.valueOf(entry.getValue());
                } else {
                    value = entry.getValue();
                }
            }
            genderCount.put(key, value);
        }
        Map<String, Integer> genderCountl = new HashMap<>();
        for (Map<String, Integer> kv : mapsl) {
            String key = null;
            Integer value = null;
            for (Map.Entry<String, Integer> entry : kv.entrySet()) {
                if (entry.getKey().equals("key")) {
                    key = String.valueOf(entry.getValue());
                } else {
                    value = entry.getValue();
                }
            }
            genderCountl.put(key, value);
        }
        Set<String> strings = genderCount.keySet();
        Set<String> stringsl = genderCountl.keySet();
        System.out.println(genderCountl);
        for (String sa : strings) {
            s2[Integer.valueOf(sa)] = genderCount.get(sa);
        }
        for (String s : stringsl) {
            s3[Integer.valueOf(s)] = genderCountl.get(s);
        }
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(s1);
        jsonArray.add(s2);
        jsonArray.add(s3);
        String result2 = jsonArray.toJSONString();
        System.out.println(result2);
        return result2;
    }

    @RequestMapping("/RecentOrders")
    @ResponseBody
    public String RecentOrders() {
        Map<String, Object> queryMap = new HashMap<>();
        List<OrderWaiter> orderWaiters = orderWaiterService.selectList(queryMap);
        String s = JSON.toJSONString(orderWaiters);
        System.out.println(s);

        return "{\"count\":" + orderWaiters.size() + ",\"code\":0 , \"data\":" + s + "}";
    }

    @RequestMapping("/SomeQueries")
    @ResponseBody
    public String SomeQueries(Model model) {
        //总订单金额
        Map<String, Object> ret = new HashMap<>();
        Map<String, Integer> allIncome = orderService.selectAllIncome();
        Integer orderAmount = allIncome.get("value");
        if (orderAmount == null)
            ret.put("orderAmount", 0);
        else ret.put("orderAmount", orderAmount);
        //今日销售额
        Map<String, Integer> toDayIncome = orderService.selectToDayIncome();
        Integer toDayAmount = toDayIncome.get("value");
        if (toDayAmount == null)
            ret.put("toDayAmount", 0);
        else ret.put("toDayAmount", toDayAmount);
        //今日活跃设备
        Map<String, Integer> toDayOrderEqu = orderService.selectToDayOrderEqu();
        Integer toDayEqu = toDayOrderEqu.get("value");
        if (toDayEqu == null)
            ret.put("toDayEqu", 0);
        else ret.put("toDayEqu", toDayEqu);
        //今日活跃用户 toDayVis
        Map<String, Integer> toDayVisiter = orderService.selectToDayVisiter();
        Integer toDayVis = toDayVisiter.get("value");
        if (toDayVis == null)
            ret.put("toDayVis", 0);
        else ret.put("toDayVis", toDayVis);
        ret.put("TotalCount", TotalCount);
        String s = JSON.toJSONString(ret);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/lateOrderGoods")
    @ResponseBody
    public String selectLateOrderGoods() {
        List<Map<String, String>> maps = orderService.selectLateOrderGoods();
        String s = JSON.toJSONString(maps);
        System.out.println(s);
        return s;
    }
}
